package com.itcast.springboot.service.impl;

import com.itcast.springboot.bean.Forward;
import com.itcast.springboot.bean.OtherComm;
import com.itcast.springboot.bean.UserDyna;
import com.itcast.springboot.mapper.ForwardMapper;
import com.itcast.springboot.mapper.OtherCommMapper;
import com.itcast.springboot.mapper.UserDynaMapper;
import com.itcast.springboot.service.ForwardService;
import com.itcast.springboot.service.OtherCommService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
//对抛出的任何异常都进行自动回滚
@Transactional(rollbackFor = Exception.class)
public class ForwardServiceImpl implements ForwardService {

    @Autowired
    ForwardMapper forwardMapper;

    @Autowired
    UserDynaMapper userDynaMapper;

    @Override
    public Integer insertForward(String fwCon, Forward forward) {
        if (forward != null) {
            //获取用户动态信息
            UserDyna userDyna = userDynaMapper.getUserDynaById(forward.getUserdynaId());
            //修改转发的用户动态信息:内容、转发用户id
            userDyna.setDynaCon(fwCon);
            userDyna.setUserId(forward.getUserId());
            userDyna.setUddate(forward.getFwdate());
            //添加到用户动态表中
            userDynaMapper.insertUserDyna(userDyna);
            //添加到转发列表中
            Integer result = forwardMapper.insertForward(forward);
            if (result != 0) {
                return forwardMapper.getCountByUserDynaId(forward.getUserdynaId());
            }
            return 0;
        }
        return 0;
    }

    @Override
    public Integer deleteForward(Integer fwid) {
        return forwardMapper.deleteForward(fwid);
    }

    @Override
    public Forward getForward(Integer fwid) {
        return forwardMapper.getForward(fwid);
    }

    @Override
    public Integer getCountByUserDynaId(Integer udid) {
        return forwardMapper.getCountByUserDynaId(udid);
    }

    @Override
    public List<Integer> getCountsByUserDynasId(List<Integer> udids) {
        return forwardMapper.getCountsByUserDynasId(udids);
    }

    @Override
    public List<Forward> getForwardsAndUsersById(List<Integer> udid) {
        return forwardMapper.getForwardsAndUsersById(udid);
    }

    @Override
    public List<Forward> getForwardsById(List<Integer> udids) {
        return forwardMapper.getForwardsById(udids);
    }

    @Override
    public List<Forward> getForwardsAndNames(List<Integer> udids) {
        return forwardMapper.getForwardsAndNamesByIds(udids);
    }

    @Override
    public Integer updateForward(Forward forward) {
        return forwardMapper.updateForward(forward);
    }
}
